//
//  TopViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/17.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "TopViewController.h"
#import "TopModal.h"
#import "TopCell.h"
#import "DataService.h"
#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface TopViewController ()

@end

@implementation TopViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"排行榜";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollectionView];
    [self _initData];
    [self _loadData];
    // Do any additional setup after loading the view.
}

- (void)_createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kwidth-20)/3,(kwidth-20)/3*1.5 );
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 10;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //注册
    UINib *nib = [UINib nibWithNibName:@"TopCell" bundle:[NSBundle mainBundle]];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _topModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TopModal *modal = _topModalArray[indexPath.row];
    cell.modal = modal;
    return cell;
}

- (void)_initData {
    _topModalArray = [[NSMutableArray alloc] init];
}
- (void)_loadData {
    
    NSDictionary *topDic = [DataService getJsonDataFromFile:@"top250.json"];
    NSArray *subjectsArray = [topDic objectForKey:@"subjects"];
    for (NSDictionary *dic in subjectsArray) {
        TopModal *modal = [[TopModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
//        NSNumber *data = dic[@"rating"][@"average"];
//        modal.average = [data floatValue];
        [_topModalArray addObject:modal];
    }

    
}
//设置边缘距离，整体的边界距离，
//
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 1 、edgesForExtendedLayout
 //这个属性应用于容器控制器的子控制器
 
 延伸方向设置，在iOS7里面导航栏是半透明，iOS7的风格是充满屏，如果是半透明的话，内容向上调整64，y的0点在屏幕0点。
 默认是4个方向延伸，充满屏幕。
 如果设置不延伸，则0点在导航栏下面。
 
 This property is only applied to view controllers that are embedded in containers, such as UINavigationController or UITabBarController. View controllers set as the root view controller do not react to this property. Default value is UIRectEdgeAll.
 
 
 
 2、automaticallyAdjustsScrollViewInsets
 这个属性用于scrollView的自动调整，如果scrollView延伸到了屏幕0点，那么内容也在0点则会把内容遮挡住，这个属性默认为yes ，scrollView的内容向下偏移64,这样就不会遮挡
 
 
 Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
 
 
 
 前提条件：
 1  半透明
 2  scrollView 是vc的第一个子视图
 
 两个设置
 1 、edgesForExtendedLayout
 //这个属性应用于容器控制器的子控制器
 
 延伸方向设置，在iOS7里面导航栏是半透明，iOS7的风格是充满屏，如果是半透明的话，内容向上调整64，y的0点在屏幕0点。
 默认是4个方向延伸，充满屏幕。
 如果设置不延伸，则0点在导航栏下面。
 
 This property is only applied to view controllers that are embedded in containers, such as UINavigationController or UITabBarController. View controllers set as the root view controller do not react to this property. Default value is UIRectEdgeAll.
 
 
 
 2、automaticallyAdjustsScrollViewInsets
 这个属性用于scrollView的自动调整，如果scrollView延伸到了屏幕0点，那么内容也在0点则会把内容遮挡住，这个属性默认为yes ，scrollView的内容向下偏移64,这样就不会遮挡
 
 
 Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
 
 
 1、KVC为modal 赋值
 //modal 中复写此方法 ，为dic中key和 modal中 变量不同名 的情况
 - (void)setValue:(id)value forUndefinedKey:(NSString *)key{
 if ([key isEqualToString:@"rating"]) {
 NSDictionary *dic = value;
 _average = [[dic objectForKey:@"average"] floatValue];
 }
 
 }
 
 
 [modal setValuesForKeysWithDictionary:dic];
 
 
 
 2、封装json数据读取类 的封装 DataService类
 //把获取json数据的代码封装起来
 3、common.h作用，定义宏
 
 
 //直接设置tableView的头视图
 - (void)_createHeaderView
 {
 
 NewsModal *headerModal = _newsModalArray[0];
 
 headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, KWidth, 40)];
 
 headerTitle.text = [NSString stringWithFormat:@"%@",headerModal.title];
 [headerTitle setTextAlignment:NSTextAlignmentCenter];
 headerTitle.font = [UIFont systemFontOfSize:18];
 headerTitle.textColor = [UIColor whiteColor];
 
 headerTitle.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自动布局，自适应顶部
 
 _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, KWidth, 200)];
 
 _imageView.contentMode = UIViewContentModeScaleAspectFill;
 
 NSString *headerImageURL = headerModal.image;
 [_imageView sd_setImageWithURL:[NSURL URLWithString:headerImageURL]];
 
 [_newsTableView addSubview:_imageView];
 
 [_imageView addSubview:headerTitle];
 
 }
 
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
 
 CGFloat y = scrollView.contentOffset.y + 64;//根据实际选择加不加上NavigationBarHight（44、64 或者没有导航条）
 if (y < -200) {
 CGRect frame = _imageView.frame;
 frame.origin.y = y;
 frame.size.height =  -y;//contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
 _imageView.frame = frame;
 }
 
 }
 
     _newsTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0); 
*/
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
